package org.rescue.data.load;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.PreparedStatement;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import javax.swing.tree.DefaultMutableTreeNode;

import org.rescue.search.Dataset;
import org.rescue.search.Document;
import org.rescue.data.extraction.Location;

public class MetadataLoader {
	String filename;
	Extractor extractor;	

	String dbname;
	String loginUser = "hurricane-dev";
	String loginPasswd = "hurricane-dev";
	//String loginUrl = "jdbc:mysql://rescue-ibm.calit2.uci.edu:3306/hurricane-dev";
	String loginUrl = "jdbc:mysql://128.195.185.173:3306/hurricane-dev2";

	String nodeTable, linkTable;
	int batchSize;
	int recCount;		

	public String load(String filename, Extractor extractor, String dbname, int batchSize) {
		if (Extractor.kind == "node") {
			nodetable = nodeLoader();
			return nodetable;
		}
		else if (Extractor.kind == "link") {
			linktable = linkLoader();
		}
	}

	public String nodeLoader() {

		NodeData nodeData;
		int i=0;	

		DB db = new DB();

		try {		      
			PreparedStatement stmt = null;
			ResultSet rs = null;

			Connection conn=db.dbConnect(
					loginUrl,loginUser,loginPasswd);
			createNodeTables();   
			
			nodeData = extractor.getNext();  

			while (nodeData.id != null) {
				if (i < batchSize) {
					// get the records
					String record = extractor.getRecordValues (nodeData); 

					/* String record = 	" values (" + Integer.toString(nodeData.concept_id) 
		            + ", '" + Integer.toString(nodeData.ancestor_id) +  
	               "')"; */

					stmt.addBatch("Insert into " + nodeTable1 + record);
					i++;
					recCount++;
				}
				else {
					int [] insertCounts = stmt.executeBatch();
					System.out.println ("inserted " + i + " records");
					i = 0;
				}
			}

			// execute the remaining batch
			int [] insertCounts = stmt.executeBatch();

			stmt.close();
			conn.close();



		} catch (SQLException ex) {
			while (ex!=null){
				System.err.println("SQLException: " + ex.getMessage());
				System.err.println("SQLState: " + ex.getSQLState());
				System.err.println("VendorError: " + ex.getErrorCode());
				ex = ex.getNextException();
			}
		}
		catch (Exception ex) {
			System.err.println("Exception: " + ex.getMessage());
		}
	}
	
	public void createNodeTables()
	{
		String query;
		Statement stmt;

		try
		{
			String schema = extractor.getRecordSchema (nodeData);        	


			/*  query="create table cust_profile " +
                "(name varchar(32), " +
                "address1 varchar(50), " +
                "city varchar(50), " +
                "state varchar(50), " +
                "country varchar(50))"; */
			stmt = conn.createStatement();
			stmt.executeUpdate("create table " + nodeTable + " " + schema);
			stmt.close();
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}	
}

